package site.addzero.cli.autoinit.common

import org.koin.core.annotation.Single
import site.addzero.cli.config.ConfigService
import site.addzero.cli.packagemanager.PackageManagerStrategty
import site.addzero.cli.platform.PlatformTools
import site.addzero.task.InstallService
import site.addzero.util.KoinInjector
import site.addzero.task.InitTask

@Single
class AutoInitPkgManager : InitTask {
    override val support: Boolean = true

    /**
     * 执行包管理器初始化任务
     */
    override suspend fun asynctask(): Boolean {
        val configService = KoinInjector.inject<ConfigService>()
        val supportPackageManager = PackageManagerStrategty.Companion.getSupportPackageManager()
        val filterNot = configService.currentPlatformConfig.defaultPackages.filterNot {
            val packageInstalled = supportPackageManager.isPackageInstalled(it)
            packageInstalled
        }
        when {
            filterNot.isNotEmpty() -> {
                val readBoolean =
                    PlatformTools.readBoolWithFlase("这些安装包还没有安装过$filterNot,是否确认安装?") ?: false
                if (readBoolean) {
                    println("开始静默安装")
                    //查看哪些包还没安装过
                    val inject = KoinInjector.inject<InstallService>()
                    inject.executeInternal()
                } else {
                    println("您选择了不安装,继续执行其他任务")
                }
            }
        }
        
        return true
    }
}
